﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlExs;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Drawing.Printing;

namespace QXJ
{
    public partial class MainWindow : FormEx
    {
        //给打印功能用的，当前是哪一个DataGridView
        enum CurrentView
        {
            DayView,
            MonthView
        };

        private CurrentView cv;

        private StringBuilder CuZanHao;
        public MainWindow()
        {
            InitializeComponent();
            ShowTime();
            pdDocument.PrintPage += new PrintPageEventHandler(pdDocument_PrintPage);
            dlgPageSetup.Document = pdDocument;
            dlgPrint.Document = pdDocument;
            dlgPrintPreD.Document = pdDocument;
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
            imageButton5_Click(null, null);
            CuZanHao = new StringBuilder((new Settings1()).区站号);
        }
        #region About UI 这块不要了，太尼玛浪费计算了
        ///// <summary>
        ///// 绘制窗体主体部分白色透明层
        ///// </summary>
        ///// <param name="form"></param>
        ///// <param name="g"></param>
        //public static void DrawFromAlphaMainPart(Form form, Graphics g)
        //{
        //    Color[] colors = 
        //    {
        //        Color.FromArgb(5, Color.White),
        //        Color.FromArgb(30, Color.White),
        //        Color.FromArgb(145, Color.White),
        //        Color.FromArgb(150, Color.White),
        //        Color.FromArgb(30, Color.White),
        //        Color.FromArgb(5, Color.White)
        //    };

        //    float[] pos = 
        //    {
        //        0.0f,
        //        0.04f,
        //        0.10f,
        //        0.90f,
        //        0.97f,
        //        1.0f      
        //    };

        //    ColorBlend colorBlend = new ColorBlend(6);
        //    colorBlend.Colors = colors;
        //    colorBlend.Positions = pos;

        //    RectangleF destRect = new RectangleF(0, 0, form.Width, form.Height);
        //    using (LinearGradientBrush lBrush = new LinearGradientBrush(destRect, colors[0], colors[5], LinearGradientMode.Vertical))
        //    {
        //        lBrush.InterpolationColors = colorBlend;
        //        g.FillRectangle(lBrush, destRect);
        //    }
        //}
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    //DrawFromAlphaMainPart(this, e.Graphics);
        //}
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        if (!DesignMode)
        //        {
        //            cp.ExStyle |= (int)WindowStyle.WS_CLIPCHILDREN;
        //        }
        //        return cp;
        //    }
        //}

        #endregion

#region 打印相关
        PrintDocument pdDocument = new PrintDocument();
        PageSetupDialog dlgPageSetup = new PageSetupDialog();
        PrintDialog dlgPrint = new PrintDialog();
        PrintPreviewDialog dlgPrintPreD = new PrintPreviewDialog();
#endregion

        #region util for panle
        /// <summary>
        /// 泛型实现单例模式
        /// </summary>
        /// <typeparam name="T">需要实现单例的类</typeparam>
        public class Singleton<T> where T : new()
        {
            /// <summary>
            /// 返回类的实例
            /// </summary>
            public static T Instance
            {
                get { return SingletonCreator.instance; }
            }

            class SingletonCreator
            {
                internal static readonly T instance = new T();
            }
        }
        public class Util
        {
            /// <summary>
            /// 把控件放到Panel里去。
            /// </summary>
            /// <param name="ctrl"></param>
            /// <param name="p"></param>
            public static void Add2Panel(Control ctrl, Panel p)
            {
                p.Controls.Clear();
                ctrl.Size = p.Size;
                ctrl.Dock = DockStyle.Fill; //居中
                //双缓冲
                p.GetType().GetMethod("SetStyle", System.Reflection.BindingFlags.Instance
                    | System.Reflection.BindingFlags.NonPublic).Invoke(p, new object[] { System.Windows.Forms.ControlStyles.UserPaint
                    | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint 
                    | System.Windows.Forms.ControlStyles.DoubleBuffer, true });
                ctrl.GetType().GetMethod("SetStyle", System.Reflection.BindingFlags.Instance
                    | System.Reflection.BindingFlags.NonPublic).Invoke(ctrl, new object[] { System.Windows.Forms.ControlStyles.UserPaint 
                    | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint 
                    | System.Windows.Forms.ControlStyles.DoubleBuffer, true });
                p.Controls.Add(ctrl);
            }
        }
        #endregion

        private void ShowTime()
        {
            Thread th = new Thread(new ThreadStart(() =>
                {
                    while (true)
                    {
                       try
                        {
                            Thread.Sleep(1000);
                            this.Invoke(new Action(() =>
                                {
                                    this.toolStripStatusLabel1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                }));
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }));
            th.IsBackground = true;
            th.Start();
           
        }

        private void ExitApp()
        {
            Application.Exit();
        }

        //exit
        private void imageButton4_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("\n确定要退出吗？                 \n\n", "提示", MessageBoxButtons.YesNo);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                ExitApp();
            }
        }
        //about
        private void imageButton3_Click(object sender, EventArgs e)
        {
            About ab = new About();
            ab.ShowDialog();
        }

        //print
        private void imageButton1_Click(object sender, EventArgs e)
        {
            try
            {
                pdDocument.Print();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, " 异常提示");

            }
        }


        void pdDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            //throw new NotImplementedException();
            int x, y;
            x = 60;
            y = 40;
            if (cv ==  CurrentView.MonthView)
            {
                PrintDataGridView11.Print(Singleton<MonthData>.Instance.dgv, true, e, ref x, ref y);
            }
            else if (cv == CurrentView.DayView)
            {
                PrintDataGridView11.Print(Singleton<DayData>.Instance.dgv, true, e, ref x, ref y);
            }
            else
            {
                throw new Exception("暂时没有可打印内容 ");
            }

        }
        //submit
        private void imageButton2_Click(object sender, EventArgs e)
        {
            string _quzhanhao = this.qqTextBox1.Text.Trim();
            DateTime _date = this.dateTimePicker1.Value;//包含了Date 和Time，但是，这里只用到Date
            if (cv == CurrentView.DayView)
            {
                Singleton<DayData>.Instance.SetDateQZH(_date, _quzhanhao);//传递信息
                Singleton<DayData>.Instance.UpdateShow();//显示
            }
            else if (cv == CurrentView.MonthView)
            {
                Singleton<MonthData>.Instance.SetQzhDate(_date, _quzhanhao);
                Singleton<MonthData>.Instance.UpdateShow();
            }else
            {
                return;
            }
        }
        //month viewer
        private void imageButton5_Click(object sender, EventArgs e)
        {
            if (this.imageButton5.Text.Equals("月视图") || this.imageButton5.Text.Equals("newstart"))
            {
                Util.Add2Panel(Singleton<DayData>.Instance, this.splitContainer1.Panel2);
                this.imageButton5.Text = @"日视图";
                this.toolStripStatusLabel2.Text = @"日视图";
                cv = CurrentView.DayView;
            }
            else
            {
                
              
                Util.Add2Panel(Singleton<MonthData>.Instance, this.splitContainer1.Panel2);
                this.imageButton5.Text = @"月视图";
                this.toolStripStatusLabel2.Text = @"月视图";
                cv = CurrentView.MonthView;
            }

        }

        //打印预览
        private void imageButton6_Click(object sender, EventArgs e)
        {
            try
            {
                dlgPrintPreD.ShowDialog();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, " 异常提示");
            }
        }
        //打印设置 
        private void imageButton7_Click(object sender, EventArgs e)
        {
            try
            {
                dlgPageSetup.ShowDialog();
                dlgPrint.ShowDialog();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, " 异常提示");

            }
        }

        //远程采集数据
        private void qqCheckBox1_CheckStateChanged(object sender, EventArgs e)
        {
            if (qqCheckBox1.CheckState == CheckState.Checked)
            {
                //采集数据
                Console.WriteLine("checked");
            }
            else
            {
                //不采集数据 
                Console.WriteLine("unchecked");
            }
        }
        //导出A数据
        private void imageButton9_Click(object sender, EventArgs e)
        {

        }

        //生成文件
        private void imageButton8_Click(object sender, EventArgs e)
        {

        }
    }
}
